<?php
namespace System\Core;

class Response {
    protected $content;
    protected $statusCode = 200;
    protected $headers = [];
    
    public function __construct($content = '', $statusCode = 200) {
        $this->content = $content;
        $this->statusCode = $statusCode;
        $this->addHeader('Content-Type', 'text/html');
    }
    
    public static function json($data, $statusCode = 200) {
        $response = new self(json_encode($data), $statusCode);
        $response->addHeader('Content-Type', 'application/json');
        return $response;
    }
    
    public function addHeader($name, $value) {
        $this->headers[$name] = $value;
        return $this;
    }
    
    public function send() {
        // 发送状态码
        http_response_code($this->statusCode);
        
        // 发送头部
        foreach ($this->headers as $name => $value) {
            header("{$name}: {$value}");
        }
        
        // 发送内容
        echo $this->content;
    }
}